package com.ruoyi.course.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.course.domain.UserCourse;
import com.ruoyi.course.mapper.ChapterMapper;
import com.ruoyi.course.mapper.UserCourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CourseMapper;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;

/**
 * 课程Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-26
 */
@Service
public class CourseServiceImpl implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private UserCourseMapper userCourseMapper;

    @Autowired
    private ChapterMapper chapterMapper;


    /**
     * 查询课程
     *
     * @param courseId 课程主键
     * @return 课程
     */
    @Override
    public Course selectCourseByCourseId(Long courseId) {
        return courseMapper.selectCourseByCourseId(courseId);
    }

    /**
     * 查询课程列表
     *
     * @param course 课程
     * @return 课程
     */
    @Override
    public List<Course> selectCourseList(Course course) {
        return courseMapper.selectCourseList(course);
    }


    /**
     * 新增课程
     *
     * @param course 课程
     * @return 结果
     */
    @Override
    public Long insertCourse(Course course) {
        Long userId = SecurityUtils.getUserId();       // 获取当前的用户id
        course.setUserId(userId);
        course.setCreateTime(DateUtils.getNowDate());
        courseMapper.insertCourse(course);
        return course.getCourseId();
    }

    /**
     * 修改课程
     *
     * @param course 课程
     * @return 结果
     */
    @Override
    public int updateCourse(Course course) {
        course.setUpdateTime(DateUtils.getNowDate());
        return courseMapper.updateCourse(course);
    }

    /**
     * 批量删除课程
     *
     * @param courseIds 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteCourseByCourseIds(Long[] courseIds) {
        Long courseId = courseIds[0];
        chapterMapper.deleteChapterByCourseId(courseId);   //删除多个课程的时候要根据课程id删除章节
        return courseMapper.deleteCourseByCourseIds(courseIds);
    }

    /**
     * 删除课程信息
     *
     * @param courseId 课程主键
     * @return 结果
     */
    @Override
    public int deleteCourseByCourseId(Long courseId) {
        chapterMapper.deleteChapterByCourseId(courseId);  //删除单个课程的时候要根据课程id删除章节
        return courseMapper.deleteCourseByCourseId(courseId);
    }


    /**
     * 查询课程列表
     *
     * @param course 课程
     * @return 课程
     */
    @Override
    public List<Course> selectCourrseListByChapterNum(Course course) {
        course.setUserId(SecurityUtils.getUserId());
        return courseMapper.selectCourrseListByChapterNum(course);
    }
}
